var isSend = false;
var reset = {
	sendEmail:function(email){
		var config ={ 
					url:"/api/send",
				  data:{"email":email},
				  callBackFunc:function(res){
					  if(res == true){
						  isSend = true;
						  //1分钟后可以重新发送
						  setTimeout(function(){
							  isSend = false;
						  },60000);
						  layer.msg("Send success !");
					  }else{
						  layer.msg("Send error !");
					  }
				  }
				};
		HD.doPost(config);
	},
	doNext:function(email,code,password){
		var config ={ 
				url:"/api/resetPwd/"+code+"/"+password,
				data:{"email":email},
			    callBackFunc:function(res){
					  if(res == true){
						  layer.msg("RESET SUCCESS !");
					  }else{
						  layer.msg("RESET ERROR !");
					  }
				  }
				};
		HD.doPost(config);
	},
}

$(function(){
	
	$("#sendEmail").click(function(){
		var email = $("#address").val();
		if(email == ''){
			layer.msg("Please enter Email Address !");
			return;
		}
		if(!validate.email(email)){
			layer.msg("email is nonlicet ！");
			return;
		}
		if(isSend){
			layer.msg("Have already sent Email !");
			return;
		}
		reset.sendEmail(email);
	})
	
	$("#reset").click(function(){
		var email = $("#address").val();
		var code = $("#code").val();
		var password = $("#password").val();
		if(email == ''){
			layer.msg("Please inter Email Address !");
		}
		if(!validate.email(email)){
			layer.msg("email is nonlicet ！");
			return;
		}
		if(code == ''){
			layer.msg("Please inter code !");
		}
		if(!validate.isFourNumber(code)){
			layer.msg("It is not four digits !");
			return;
		}
		if(password == ''){
			layer.msg("Please inter password !");
		}
		if(!validate.pssword(password)){
			layer.msg("password is nonlicet ！At least 6-12 characters,includeing one number/one uppercse letter/one lowerercase letter.");
			return;
		}
		reset.doNext(email,code,password);
	})
	
	$("#toLogin").click(function(){
		window.location.href = '/login'
	})
	
	//点击注册
	$("#toRegister").click(function(){
		window.location.href = '/index/toRegister';
	})
})